<?php

class UnContainerPlanApp extends MemberbaseApp
{
	function index()
	{
		$sqldb=&sqldb();

		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);

		$k = empty($_REQUEST['k']) ? '' : $_REQUEST['k'];

		$conditions = " and 1=1 AND ISNULL(B.ZG_DD,GETDATE())>=CONVERT(VARCHAR(20),GETDATE()-7,20)";
		if($k){
			$conditions.=" and (a.SHIP_OVOY like '%".$k."%' or a.TD_NO like '%".$k."%'or a.BOX_NO like '%".$k."%')";
		}

		$db=&db();
		$sql="select depot,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		$page = $this->_get_pagen(10);

		$sql="SELECT * from (SELECT *,ROW_NUMBER() OVER (ORDER BY x.SHIP_OVOY,x.TD_NO,x.BOX_NO desc) AS RowNumber FROM (SELECT DISTINCT A.BOX_NO --箱号
,A.CUS_NO --箱属
,A.CC
,A.XX --箱型
,A.ZTD_NO as TD_NO --提单号
,dbo.PT_UBOXINFO(a.ztd_no,'CMHC','','','') SHIP_OVOY --船次
,A.SEAL_NO --铅封号,
,dbo.PT_UBOXINFO(a.ztd_no,'ZGDD','','','') JGSJ --截港时间
,dbo.PT_UBOXINFO(a.ztd_no,'CQ','','','') LGSJ --离港时间
FROM T_STOCK A INNER JOIN T_UBOXPLAN B ON A.ZTD_NO=B.TD_NO
LEFT JOIN T_MF_SHIPP C ON A.SHIP_OVOY=C.SHIP_OVOY
WHERE (B.USR='".$u['user_name']."' OR ISNULL(B.MK_USR,'')='".$u['user_name']."') AND ISNULL(A.B_FLAG,'')<>'#' and isnull(JG_FLAG,'')!='Y' $conditions) x)y where RowNumber > ".$page['s']." 
		AND RowNumber <= ".$page['e']." order by SHIP_OVOY,TD_NO,BOX_NO desc";

		$plan=$sqldb->getAll($sql);
		$list=array();
		if($plan){
			foreach ($plan as $v){
				if($v['EC_DD']){
					$v['EC_DD']=gmstr2time($v['EC_DD']);
				}

				$list[]=$v;
			}
		}

		$sql="SELECT count(1) from (
		SELECT DISTINCT A.BOX_NO --箱号
,A.CUS_NO --箱属
,A.CC
,A.XX --箱型
,A.ZTD_NO as TD_NO --提单号
,dbo.PT_UBOXINFO(a.ztd_no,'CMHC','','','') SHIP_OVOY --船次
,A.SEAL_NO --铅封号,
,dbo.PT_UBOXINFO(a.ztd_no,'ZGDD','','','') JGSJ --截港时间
,dbo.PT_UBOXINFO(a.ztd_no,'CQ','','','') LGSJ --离港时间
FROM T_STOCK A INNER JOIN T_UBOXPLAN B ON A.ZTD_NO=B.TD_NO
LEFT JOIN T_MF_SHIPP C ON A.SHIP_OVOY=C.SHIP_OVOY
WHERE (B.USR='".$u['user_name']."' OR ISNULL(B.MK_USR,'')='".$u['user_name']."') AND ISNULL(A.B_FLAG,'')<>'#' and isnull(JG_FLAG,'')!='Y' $conditions) x";
		$c=$sqldb->getOne($sql);
		//$this->json_result($list);

		$page['item_count'] = $c;
		$this->_format_pageweb($page);
		$this->assign('page_info', $page);

		$this->assign('plan', $list);
		$this->assign('d', time());
		$this->display('uncontainerplan.html');
	}

	function jg(){
		$sqldb=&sqldb();
		$his= empty($_REQUEST['his']) ? '' : $_REQUEST['his'];
		if(empty($his)){
			$this->pons_error('info','您没有选中任何数据');
			exit();
		}
		$his=array_values($his);
		$uid=$this->visitor->get("user_id");

		$db=&db();
		$sql="select depot,del,company,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		if($u['del']){
			$this->pons_error('info','用户被禁用，如有疑问请联系客服');
			exit();
		}

		if(!$u['depot']){
			$this->pons_error('info','用户还没有被系统确认，无法生成');
			exit();
		}

		$sql="select * from T_SALM where SAL_NO='".$u['depot']."'";
		$salm=$sqldb->getRow($sql);

		if(!$salm){
			$this->pons_error('info','用户还没有被系统确认，无法生成');
			exit();
		}

		foreach ($his as $vv){
			if(empty($vv['box'])){
				continue;
			}

			$sql="select A.BOX_NO,A.XX,A.CC,A.CUS_NO,A.SHIP_OVOY,A.TD_NO,A.ET_NO 
			FROM T_STOCK A INNER JOIN T_UBOXPLAN B ON A.ZTD_NO=B.TD_NO 
			where B.USR='".$u['user_name']."' AND ISNULL(A.B_FLAG,'')<>'#' and A.BOX_NO='".$vv['box']."' and isnull(JG_FLAG,'')!='Y'";
			$v=$sqldb->getRow($sql);
				
			if(empty($v)){
				continue;
			}
				
			$BOX_NO= empty($v['BOX_NO']) ? '' : $v['BOX_NO'];
			$CUS_NO = empty($v['CUS_NO']) ? '' : $v['CUS_NO'];
			$XX= empty($v['XX']) ? '' : $v['XX'];
			$CC= empty($v['CC']) ? '' : $v['CC'];
			$SHIP_OVOY= empty($v['SHIP_OVOY']) ? '' : $v['SHIP_OVOY'];
			$TD_NO= empty($v['TD_NO']) ? '' : $v['TD_NO'];
			$ET_NO= empty($v['ET_NO']) ? '' : $v['ET_NO'];
			$JG_DD= empty($vv['JG_DD']) ? '' : $vv['JG_DD'];
			$REM= empty($vv['REM']) ? '' : $vv['REM'];
			if(!$JG_DD){
				$this->pons_error('info','请选择截港时间');
				exit();
			}

			/*$sql="insert into T_TF_JBOXPLAN(VC_NO,SHIP_NO,TD_NO,XX,QTY,JG_DD,REM,USR,COMP,COMP_NO,MK_DD,CUS_NO,ET_NO,BOX_NO,CC)
		values('".$this->create_guidnew()."','".$SHIP_OVOY."','".$TD_NO."','".$XX."','1','".$JG_DD."',
		'".$REM."','".$u['user_name']."','".$salm['COMP']."','".$salm['COMPNO']."',
		'".local_date('Y-m-d H:i:s',gmtime())."','".$CUS_NO."','".$ET_NO."','".$BOX_NO."','".$CC."')";
			$sqldb->query($sql);*/
			
			$sql="UPDATE T_STOCK SET JG_FLAG='Y',JP_DD='".$JG_DD."',REM1='".$REM."',XJ_DD=GETDATE() WHERE BOX_NO='".$BOX_NO."'";
			$sqldb->query($sql);
		}

		$ret_url="index.php?app=containerplan";
		$this->pons_result($ret_url);
		exit();
	}
}

?>
